package cn.candy.user.controller;

import cn.candy.basic.exception.BusinessExcpetion;
import cn.candy.basic.util.AjaxResult;
import cn.candy.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * 短信验证码接口类
 */
@RestController
@RequestMapping("/verifycode")
@Api(tags = "短信验证码接口类",description = "短信验证码接口详细描述")
public class VerifycodeController {

    @Autowired
    private IUserService userService;

    /**
     * 手机注册：发送验证码接口
     * @param map - 接口参数【代替对象】
     * @return
     */
    @PostMapping("/smsCode")
    @ApiOperation(value = "手机注册：发送短信接口",notes = "")
    public AjaxResult sendSmsCode(@RequestBody Map<String,String> map){
        try {
            String phone = map.get("phone");
            userService.sendSms(phone);
            return new AjaxResult();
        } catch (BusinessExcpetion e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统异常，" + e.getMessage());
        }
    }


    /**
     * 微信用户绑定：发送验证码接口
     * @param map - 接口参数【代替对象】
     * @return
     */
    @PostMapping("/binderCode")
    @ApiOperation(value = "微信用户绑定：发送短信接口",notes = "")
    public AjaxResult sendBinderCode(@RequestBody Map<String,String> map){
        try {
            String phone = map.get("phone");
            userService.sendBinderCode(phone);
            return new AjaxResult();
        } catch (BusinessExcpetion e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统异常，" + e.getMessage());
        }
    }
}
